package com.worldtolearn.offer.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.stereotype.Controller;

import com.worldtolearn.offer.bu.ActivityBu;
import com.worldtolearn.offer.bu.AdressBu;
import com.worldtolearn.offer.bu.IActivityBu;
import com.worldtolearn.offer.bu.IAdressBu;
import com.worldtolearn.offer.bu.IOfferBu;
import com.worldtolearn.offer.bu.OfferBu;
import com.worldtolearn.offer.entity.Offer;
import com.worldtolearn.user.bean.LoginMB;

@Controller
@ManagedBean
@Named
@SessionScoped
public class OffersListMb implements Serializable {

	private static final long serialVersionUID = 1L;

	@Inject
	IOfferBu offerBu;

	@Inject
	IAdressBu adressBu;
	
	@Inject
	IActivityBu activityBu;

	
	List<Offer> listOffer = new ArrayList<Offer>();
	List<Offer> listOfferSearch = new ArrayList<Offer>();

	List<String> listActivityLabel = new ArrayList<String>();
	List<String> listPays = null;

	String activityChoisie = " ";
	String paysChoisi = " ";	
	
	Offer offer = new Offer();

	// *******************************initialisation de la liste
	// d'offre***********************************
	@PostConstruct
	public void initListMb() {
		listOffer = offerBu.getAllOffers();
		Map<String, String> tempCountry = new Hashtable<String, String>();
		for (Offer offer : listOffer) {
			
			
			String country = offer.getSupplierOffer().getAdress().getCountry();
			tempCountry.put(" ", " ");
			tempCountry.put(country, country);
			
		}
		listPays = new ArrayList<String>(tempCountry.values());
		listActivityLabel = activityBu.getDistinctsActivity();
		listActivityLabel.add(0, " ");
	}

	// *******************tri de la liste d'offre en fonction de
	// l'activité********************
	public void ValueChangeEventActivity(ValueChangeEvent event) {
		FacesContext context = FacesContext.getCurrentInstance();
		activityChoisie = (String) event.getNewValue();
		System.out.println("Activité choisie " + activityChoisie);
		
		if (!activityChoisie.equals(" ")) {
			listOffer = offerBu.getOfferByActivity(listOffer, activityChoisie);
			listPays.clear();
			listPays = adressBu.getDistinctAdresseFromList(listOffer);
		}
		if(activityChoisie.equals(" ") && !paysChoisi.equals(" ")) {
			System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++"+paysChoisi);
			listOffer = offerBu.getAllOffers();
			listPays = adressBu.getDistinctAdresseFromList(listOffer);
			listOffer = offerBu.getOfferBycountry(listOffer, paysChoisi);			
		}
		if(activityChoisie.equals(" ") && paysChoisi.equals(" ")) {
			listOffer = offerBu.getAllOffers();
			listPays = adressBu.getDistinctAdresseFromList(listOffer);
		}
//		if(!activityChoisie.equals(" ") && !paysChoisi.equals(" ")) {
//			String [] list = {activityChoisie, paysChoisi};
//			listOffer = offerBu.getOfferBySearchCriterion(listOffer, list);
//			listPays = adressBu.getDistinctAdresseFromList(listOffer);
//		}
		
		listPays.add(0, " ");
	}

	// *******************tri de la liste en fonction du pays
	public void ValueChangeEventCountry(ValueChangeEvent event) {
		FacesContext context = FacesContext.getCurrentInstance();
		paysChoisi = (String) event.getNewValue();
		if (!paysChoisi.equals(" ") && !activityChoisie.equals(" ")) {
			String [] list = {activityChoisie, paysChoisi};
			listOffer = offerBu.getOfferBySearchCriterion(listOffer, list);
		}
		if (!paysChoisi.equals(" ") && activityChoisie.equals(" ")) {
			listOffer = offerBu.getOfferBycountry(listOffer, paysChoisi);			
		}
		if (paysChoisi.equals(" ") && !activityChoisie.equals(" ")) {
			listOffer = offerBu.getOfferByActivity(listOffer, activityChoisie);
		}
		if (paysChoisi.equals(" ") && activityChoisie.equals(" ")){
			listOffer = offerBu.getAllOffers();
		}
	}
	
	public void getOfferDetails(ActionEvent event) {
		int idOffer = (Integer) event.getComponent().getAttributes().get("offerId");
		listOfferSearch = offerBu.getOfferById(idOffer);
		offer = listOfferSearch.get(0);
	}

	public List<String> getListActivityLabel() {
		return listActivityLabel;
	}

	public void setListActivityLabel(List<String> listActivityLabel) {
		this.listActivityLabel = listActivityLabel;
	}

	public String getActivityChoisie() {
		return activityChoisie;
	}

	public void setActivityChoisie(String activityChoisie) {
		this.activityChoisie = activityChoisie;
	}

	public void setListOffer(List<Offer> listOffer) {
		this.listOffer = listOffer;
	}

	public List<Offer> getListOfferSearch() {
		return listOfferSearch;
	}

	public void setListOfferSearch(List<Offer> listOfferSearch) {
		this.listOfferSearch = listOfferSearch;
	}


	public void setAdressBu(AdressBu adressBu) {
		this.adressBu = adressBu;
	}

	public List<String> getListPays() {
		return listPays;
	}

	public void setListPays(List<String> listPays) {
		this.listPays = listPays;
	}

	public String getPaysChoisi() {
		return paysChoisi;
	}

	public void setPaysChoisi(String paysChoisi) {
		this.paysChoisi = paysChoisi;
	}

	public List<Offer> getListOffer() {
		return listOffer;
	}

	

	public void setOfferBu(OfferBu offerBu) {
		this.offerBu = offerBu;
	}

	public List<String> getListActivity() {
		return listActivityLabel;
	}

	

	public void setActivityBu(ActivityBu activityBu) {
		this.activityBu = activityBu;
	}

	public Offer getOffer() {
		return offer;
	}

	public void setOffer(Offer offer) {
		this.offer = offer;
	}

	public String getCurrency(){
	    LoginMB loginMB = (LoginMB) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("loginMB");
	    
//	    HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance()
//                .getExternalContext().getRequest();
//	    
//	    for(@SuppressWarnings("rawtypes")
//      Enumeration en = request.getSession().getAttributeNames();
//      en.hasMoreElements();){
//          String elem = en.nextElement().toString();
//          System.out.println(elem);
////          if(!elem.equals("shoppingCartMB"))
////              request.getSession().setAttribute(elem, null);
//      }
	    
	    return loginMB.getCurrency();
	}

	public void setOfferBu(IOfferBu offerBu) {
		this.offerBu = offerBu;
	}

	public void setAdressBu(IAdressBu adressBu) {
		this.adressBu = adressBu;
	}

	public void setActivityBu(IActivityBu activityBu) {
		this.activityBu = activityBu;
	}
	
	
	
}
